// pages/photoSign/photoSign.js
Page({



  /**
   * 页面的初始数据
   */
  data: {
    userId:'',
    signId:'',
    signTime:'',

    isshowpic:false,
    isUseCamera: false,
    face: '',

    lng: 0,
    lat: 0,
    markers:[],
    circles:[],
    signaddress:'',

    toast: false,
    hideToast: false
  },
  
  showCamera() {
    this.setData({
      isUseCamera: true
    })
  },
  takePhoto() {
    this.setData({
      isUseCamera: true
    })
    // 得到相机上下文。可以想像成为这个context就是相机的按键面板
    const ctx = wx.createCameraContext();
    // 在按键面板上按了下快门键
    ctx.takePhoto({
      complete: (data) => {
        console.log(data)
        // 用条件渲染拿掉相机
        this.setData({
          isshowpic:true,
          isUseCamera: false
        });
        // 将图片的地址设置为本次拍摄的图片的地址
        this.setData({
          face: data.tempImagePath
        });
      }
    })
  },
  uploadFace() {
    let data = this;
    wx.uploadFile({
      url: 'http://localhost/public/sign/sign/mixsign',
      filePath: this.data.face,
      name: 'file',
      formData: {
        'user_Id':this.data.userId,
        'sign_Id':this.data.signId,
        'sign_Time':this.data.signTime,
        'signaddress': this.data.signaddress
      },
      success(res) {
        data.setData({
          toast: true
        });
        setTimeout(() => {
          data.setData({
              hideToast: true
          });
          setTimeout(() => {
              data.setData({
                  toast: false,
                  hideToast: false,
              });
          }, 300);
        }, 3000);
      }
    })
  },
  
  onLoad: function (options) {
    console.log(options);
    this.setData({
      userId:options.userId,
      signId:options.signId,
      signTime:options.signTime,
    })
    wx.getLocation({
      type: 'gcj02',
      success:(ret)=>{
          // console.log(ret)
          var marker = {
              id:1,
              longitude:ret.longitude,
              latitude:ret.latitude,
              title: "I am here",
              width:50,height:50
          }
          var longitude = ret.longitude;
          var latitude = ret.latitude;
          const key = 'CUABZ-QRALW-ZJCRC-RYLET-E75SE-FLFMJ';
          wx.request({
              type: 'gcj02',
              url:'https://apis.map.qq.com/ws/geocoder/v1',
              data:{
                  key:key,
                  location:latitude+','+longitude
              },success:(res)=>{
                  var address = res.data.result.address;
                  var name = res.data.result.formatted_addresses.recommend;
                  // const {recommend} = res.data.result.formatted_addresses
                  console.log(address,name)
                  this.setData({
                      signaddress:name,
                  })
              },fail(res){
                  console.log(res)
              }
          })
          this.setData({
              lng:ret.longitude,
              lat:ret.latitude,
              markers:[marker],
          })
      }
  })
  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})